package cn.spring.ly.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.spring.ly.entity.SysPressm;
import cn.spring.ly.entity.SysUser;
import cn.spring.ly.entity.SysUserPressm;
import cn.spring.ly.entity.vo.SysUserPressmVo;
import cn.spring.ly.page.PageInfo;
import cn.spring.ly.result.AjaxResult;
import cn.spring.ly.service.SysPressmService;
import cn.spring.ly.service.SysUserPressmService;
import cn.spring.ly.service.UserService;

@Controller
public class UserController {

	@Autowired
	UserService uerService;

	@Autowired
	SysPressmService sysPressmService;

	@Autowired
	SysUserPressmService sysUserPressmService;
	
	@RequestMapping("/sys/user/list")
	public String list(Model model, @RequestParam(defaultValue = "1", required = false) Integer pageNo) {
		PageInfo<SysUser> pageInfo = new PageInfo<>(pageNo, 10);
		pageInfo = (PageInfo<SysUser>) uerService.page(pageInfo);

		model.addAttribute("pageInfo", pageInfo);
		return "user_list";
	}

	@ResponseBody
	@RequestMapping("/sys/user/pressm/{userId}")
	public List<SysPressm> pressm(@PathVariable Long userId) {
		List<SysPressm> pressmList = sysPressmService.selectPressm(userId);
		return pressmList;
	}
	
	@RequestMapping("/sys/user/getPressm/{userId}")
	public String getPressm(@PathVariable Long userId, Model model) {
		List<SysPressm> pressmList = sysPressmService.selectPressm(userId);
		model.addAttribute("pressmList", pressmList);
		model.addAttribute("userId", userId);
		return "user_pressm";
	}
	
	@ResponseBody
	@RequestMapping("//sys/user/pressm/save")
	public AjaxResult savePressm(Long userId,Long [] pressmId){
		QueryWrapper<SysUserPressm> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(SysUserPressm.USER_ID, userId);
		//先删除
		sysUserPressmService.remove(queryWrapper);
		
		 List<SysUserPressm> pressmList = new ArrayList<SysUserPressm>();
		 for (Long presid : pressmId) {
			 SysUserPressm sysUserPressm = new SysUserPressm();
			 sysUserPressm.setPressmId(presid);
			 sysUserPressm.setUserId(userId);
			 pressmList.add(sysUserPressm);
		}
		 
		boolean b = sysUserPressmService.saveBatch(pressmList);
		if(b){
			return AjaxResult.succes("保存成功");
		}
		return AjaxResult.error("保存失败");
	}

	@RequestMapping("/")
	public String index() {

		return "login";
	}

	@RequestMapping("/logout")
	public String logout() {
		SecurityUtils.getSubject().logout();
		return "login";
	}

	/**
	 * 登录
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String username, String password, Model model) {

		try {
			Subject subject = SecurityUtils.getSubject();
			// 在认证提交前准备 token（令牌）
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			// 执行认证登陆
			subject.login(token);

			// 登录成功
			return "index";
		} catch (AuthenticationException e) {
			System.out.println(e);
			model.addAttribute("error", e.getMessage());
			e.printStackTrace();
		}
		return "login";
	}
}
